package controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import zxw.domian.VO;
import zxw.domian.user;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/wuhu")
public class UserController {

    @RequestMapping("/save2")
    public ModelAndView save2() {
        ModelAndView modelAndView = new ModelAndView();
        //设置视图名称
        modelAndView.setViewName("success");
        //设置模型数据
        modelAndView.addObject("username", "张学威真帅");
        return modelAndView;
    }

    @RequestMapping(value = "/save", method = RequestMethod.GET, params = {"username"})//设置请求映射
    public String save() {
        System.out.println("save。。。调用SpringMVc");
        //return "forward:/success.jsp";//转发
        return "redirect:/jsp/success.jsp";//重定向
    }

    //将modelAndView以传参的形式写入
    @RequestMapping("/save3")
    public ModelAndView save3(ModelAndView modelAndView) {
        //设置视图名称
        modelAndView.setViewName("success");
        //设置模型数据
        modelAndView.addObject("username", "还可以吧");
        return modelAndView;
    }

    @RequestMapping("/save4")
    public String save4(Model model) {
        model.addAttribute("username", "云飞扬");
        return "success";
    }

    //使用非框架知识
    @RequestMapping("/request")
    public String RequestDemo(ServletRequest request) {
        request.setAttribute("username", "request");
        return "success";
    }

    @RequestMapping("save5")
    public void save5(ServletResponse response) throws IOException {
        response.getWriter().write("hello world");
    }


    @RequestMapping("save6")
    @ResponseBody//告诉Spring-MVC该方法体直接返回字符串，不进行页面跳转
    public String save6() throws IOException {
        return "hello 直接返回";
    }

    @RequestMapping("save7")
    @ResponseBody//告诉Spring-MVC该方法体直接返回字符串，不进行页面跳转
    public String save7() throws IOException {
        user user = new user();
        user.setName("wangming");
        user.setAge(12);
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(user);
        return json;
    }

    @RequestMapping("/save8")
    @ResponseBody
    public user save8() {
        user user = new user();
        user.setAge(12);
        user.setName("zhangsan");
        return user;
    }

    @RequestMapping("/save9")
    @ResponseBody
    public void show(String name, int age) {
        System.out.println(name);
        System.out.println(age);
    }

    @RequestMapping("/save10")
    @ResponseBody
    public void save10(user user)
    {
        System.out.println(user);
    }

    @RequestMapping("/save11")
    @ResponseBody
    public void save11(String a[]) {
        System.out.println(Arrays.asList(a));
    }

    @RequestMapping("/save12")
    @ResponseBody
    public void save12(VO vo) {
        System.out.println(vo);
    }

    @RequestMapping("/save13")
    @ResponseBody
    public void save13(@RequestBody List<user> list) {
        System.out.println(list);
    }
    @RequestMapping(value = "/save14")
    @ResponseBody
    public void save14(@RequestParam(value = "name",required = false,defaultValue = "芜湖云飞扬 ") String name){
        System.out.println(name);
    }

    @RequestMapping(value = "/save15/{name}")
    @ResponseBody
    public void save15(@PathVariable(value = "name",required = true) String name){
        System.out.println(name);
    }
    @RequestMapping(value = "/save16")
    @ResponseBody
    public void save16(Date date){
        System.out.println(date);
    }
    @RequestMapping(value = "/save17")
    @ResponseBody
    public void save17(HttpServletRequest request, HttpServletResponse response, HttpSession session){
        System.out.println(request);
        System.out.println(request);
        System.out.println(response);
    }
    @RequestMapping("/save18")
    @ResponseBody
    public void save18(@RequestHeader(value = "Cookie") String name){
        System.out.println(name);
    }
    @RequestMapping("/save19")
    @ResponseBody
    public void save19(@CookieValue(value = "JSESSIONID") String name){
        System.out.println(name);
    }
    @RequestMapping("/save20")
    @ResponseBody
    public void save20(@CookieValue(value = "JSESSIONID") String name){
        System.out.println(name);
    }
    @RequestMapping("/save21")
    @ResponseBody
    public void Save21(String name, MultipartFile uploadFile,MultipartFile uploadFile2) throws IOException {
        System.out.println(name);
        String originalFilename = uploadFile.getOriginalFilename();
        uploadFile.transferTo(new File("D:\\我的文件\\"+originalFilename));
        String originalFilename2 = uploadFile2.getOriginalFilename();
        uploadFile2.transferTo(new File("D:\\我的文件\\"+originalFilename2));
    }
    @RequestMapping("/save22")
    @ResponseBody
    public void Save22(String name, MultipartFile uploadFile[]) throws IOException {
        System.out.println(name);
       for (MultipartFile multipartFile:uploadFile){
           String originalFilename = multipartFile.getOriginalFilename();
           multipartFile.transferTo(new File("D:\\我的文件\\"+originalFilename));
       }
    }

}
